package com.carrent.stat.controller;

import com.carrent.stat.domain.BaseEntity;
import com.carrent.stat.service.StatService;
import com.sun.mail.imap.protocol.FLAGS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("stat")
public class StatController {

    @Autowired
    private StatService statService;
    @RequestMapping("toCustomerAreaStat")
    public String toCustomerAreaSet(){
        return "stat/customerAreaStat";
    }

    /**
     * 跳转到业务员年度业务统计图
     * @return
     */
    @RequestMapping("toOpernameYearGradeStat")
    public String toOpernameYearGradeStat(){
        return "stat/opernameYearGradeStat";
    }
    /**
     * 跳转到公司年度业务统计图
     * @returntoCompanyYearGradeStat
     */
    @RequestMapping("toCompanyYearGradeStat")
    public String toCompanyYearGradeStat(){
        return "stat/companyYearGradeStat";
    }

    /**
     * 加载客户地区数据
     */
    @RequestMapping("loadCustomerAreaStatJson")
    @ResponseBody
    public List<BaseEntity> loadCustomerAreaStatJson(){
        return this.statService.laodCustomerAreaStatJson();
    }
    /**
     * 加载业务员年度业务统计数据
     */
    @RequestMapping("loadOpernameYearGradeStat")
    @ResponseBody
    public Map<String,Object> opernameYearGradeStat(String year){
        List<BaseEntity> entities = statService.opernameYearGradeStat(year);
        Map<String,Object> map = new HashMap<String,Object>();
        List<String> names = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for (BaseEntity entity:entities){
            names.add(entity.getName());
            values.add(entity.getValue());
        }
        map.put("name",names);
        map.put("value",values);
        return map;
    }
    /**
     * 加载公司年度业务统计数据
     */
    @RequestMapping("loadCompanyYearGradeStat")
    @ResponseBody
    public List<Double> companyYearGradeStat(String year){
        int flag = 0;
        List<Double> values = statService.companyYearGradeStat(year);
            for (int i = 0; i < values.size(); i++) {
                if(null==values.get(i)){
                    values.set(i,0.0);
                }
            }
        return values;
    }

    /**
     * 加载二维码图片
     */
    @RequestMapping("code")
    public String loadZXingImage(){
        return "sxt/image";
    }

    /**
     * 加载二维码图片
     */
    @RequestMapping("codeLogo")
    public String loadZXingImageWithLogo(){
        return "sxt/imageWithLogo";
    }

}